﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Apps.CodeGenerator
{
    public class CodeHelper
    {
        //获取前缀
        public static string GetLeftStr(string tableName)
        {
            //生成代码
            string nameSpace = "";
            if (tableName.IndexOf("_") > 0)
            {
                nameSpace = tableName.Substring(0, tableName.IndexOf("_"));
            }
            else
            {
                nameSpace = "Sys";
            }
            return nameSpace;
        }

        /// <summary>
        /// 获取datagrid列表
        /// </summary>
        /// <param name="fields"></param>
        /// <returns></returns>
        public static string GetColumns(List<CompleteField> fields)
        {
            StringBuilder sb = new StringBuilder();
            foreach (CompleteField field in fields)
            {
                //主键，隐藏
                if (field.name == "Id")
                {
                    sb.Append("                { field: '" + field.name + "', title: '" + (field.remark == "" ? field.name : field.remark) + "', width: 80,hidden:true },\r\n");
                }
                else if (field.name == "IsDel")
                {
                    continue;
                }//布尔类型，加formatter
                else if (field.xType == "104" || field.xType == "bool")
                {
                    sb.Append("                { field: '" + field.name + "', title: '" + (field.remark == "" ? field.name : field.remark) + "', width: 40,sortable:true,align:'center', formatter: function (value) {return EnableFormatter(value)}},\r\n");
                }
                else if (field.name.ToLower().Contains("img") || field.name.ToLower().Contains("photo"))
                {
                    sb.Append("                { field: '" + field.name + "', title: '" + (field.remark == "" ? field.name : field.remark) + "', width: 40,sortable:true, align: 'center', formatter: function (value, row, index) {return '<img width=\"80px\" alt=\"example\" src=\"' + value + '\" />';}},\r\n");
                }

                else
                {
                    sb.Append("                { field: '" + field.name + "', title: '" + (field.remark == "" ? field.name : field.remark) + "', width: 80,sortable:true },\r\n");
                }

            }
            return sb.ToString();
        }


        public static string GetCreateViewInitVal(List<CompleteField> fields)
        {
            StringBuilder sb = new StringBuilder();
            foreach (CompleteField field in fields)
            {

                if (field.name == "Id")
                {
                    if (field.xType != "56" && field.xType != "127")//非int型主键
                    {
                        sb.Append("             @Html.HiddenFor(model => model." + field.name + ")\r\n");
                    }
                    else
                    {
                        sb.Append("             @Html.HiddenFor(model => model." + field.name + ", new { @Value = 0})\r\n");
                    }
                }
                if (field.name == "CreateTime")
                {
                    sb.Append("             <input id=\"CreateTime\" type=\"hidden\" name=\"CreateTime\" value=\"2000-1-1\" />\r\n");
                }
                if (field.name == "UpdateTime")
                {
                    sb.Append("             <input id=\"UpdateTime\" type=\"hidden\" name=\"UpdateTime\" value=\"\" />\r\n");
                }
                if (field.name == "Updater")
                {
                    sb.Append("             <input id=\"Updater\" type=\"hidden\" name=\"Updater\" value=\"\" />\r\n");
                }
                if (field.name == "Creator")
                {
                    sb.Append("             <input id=\"Creator\" type=\"hidden\" name=\"Creator\" value=\"\" />\r\n");
                }
                if (field.name == "ModifyTime")
                {
                    sb.Append("             <input id=\"ModifyTime\" type=\"hidden\" name=\"ModifyTime\" value=\"\" />\r\n");
                }
                if (field.name == "Modifier")
                {
                    sb.Append("             <input id=\"Modifier\" type=\"hidden\" name=\"Modifier\" value=\"\" />\r\n");
                }
                if (field.name == "ParentId")
                {
                    sb.Append("             @Html.HiddenFor(model => model." + field.name + ")\r\n");
                }
            }
            return sb.ToString();
        }



        public static string GetCreateViewEditVal(List<CompleteField> fields)
        {
            StringBuilder sb = new StringBuilder();
            foreach (CompleteField field in fields)
            {

                if (field.name == "CreateTime")
                {
                    sb.Append("            model.CreateTime = ResultHelper.NowTime;\r\n");
                }
                else if (field.name == "Creator")
                {
                    sb.Append("            model.Creator = GetUserId();\r\n");
                }
                else if (field.name == "Creater")
                {
                    sb.Append("            model.Creater = GetUserId();\r\n");
                }
                else if (field.name == "IsDel")
                {
                    sb.Append("            model.IsDel = false;\r\n");
                }
            }
            return sb.ToString();
        }
    }
}